package com.lanou.musicbox.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.lanou.musicbox.R;
import com.lanou.musicbox.adapter.skin.SkinGridViewAdapter;
import com.lanou.musicbox.base.BaseActivity;
import com.lanou.musicbox.base.BaseApplication;
import com.lanou.musicbox.bean.MineSkin;
import com.lanou.musicbox.utils.MyConfig;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/10/31.
 */
public class SkinActivity extends BaseActivity implements View.OnClickListener {
    private MyConfig mSetting = null;
    private LinearLayout main_skin_rewind;
    private ImageButton skin_comback;
    private MainActivity activity;
    /**
     * 记录上一次点击的view/id
     */
    private GridView gridView;
    private List<MineSkin> mineSkins = new ArrayList<>();
    private SkinGridViewAdapter adapter;
    {
        for (int i = 0; i < BaseApplication.imgIds.length; i++) {
            MineSkin wallpaper = new MineSkin();
            wallpaper.setSkinImgResId(BaseApplication.imgIds[i]);
            wallpaper.setSkinTitleResID(BaseApplication.titleIds[i]);
            mineSkins.add(wallpaper);
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.skin_mine);
        gridView = (GridView) findViewById(R.id.skin_mine_gridView);
        main_skin_rewind = (LinearLayout) findViewById(R.id.main_skin_rewind);
        skin_comback = (ImageButton) findViewById(R.id.skin_comback);
        skin_comback.setOnClickListener(this);
        activity=new MainActivity();
        mSetting = new MyConfig(this, true);
        adapter = new SkinGridViewAdapter(this, mineSkins, mSetting.getCurrentSkinResID());
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.setCurrentId(position);

                getSkin_backgroud().setBackgroundResource(BaseApplication.imgSrcIds[position]);

                //保存点击事件后的数据
                mSetting.setCurrentSkinResId(position);

            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            toMain();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mSetting = new MyConfig(this, false);
        Log.e("XXXXXX", "-------------jin");
        main_skin_rewind.setBackgroundResource(mSetting.getCurrentSkinResID());
    }

    private void toMain() {
        // TODO Auto-generated method stub
        Intent intent = new Intent(SkinActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    public LinearLayout getSkin_backgroud() {
        return main_skin_rewind;
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.skin_comback:
            toMain();
                break;
        }
    }
}
